package org.etcsoft.examples.genealogy.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.CompoundBorder;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.widgets.Display;
import org.etcsoft.examples.genealogy.listener.FigureMover;

/**
 * @author korman21@gmail.com
 *
 */
public class PersonFigure extends RectangleFigure {
    public static final Image MALE = new Image(Display.getCurrent(),
            PersonFigure.class.getResourceAsStream("male.png"));
    public static final Image FEMALE = new Image(Display.getCurrent(),
            PersonFigure.class.getResourceAsStream("female.png"));

    public PersonFigure(String name, Image image, int birthYear, int deathYear) {
        final ToolbarLayout layout = new ToolbarLayout();
        layout.setSpacing(1);
        this.setLayoutManager(layout);
        this.setPreferredSize(100, 100);
        this.setBorder(new CompoundBorder(
                new LineBorder(1),
                new MarginBorder(2, 2, 2, 2)));

        IFigure imageNameDates = new Figure();
        final GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        gridLayout.horizontalSpacing = 1;
        imageNameDates.setLayoutManager(gridLayout);
        this.add(imageNameDates);

        imageNameDates.add(new ImageFigure(image));

        IFigure nameDates = new Figure();
        nameDates.setLayoutManager(new ToolbarLayout());
        nameDates.add(new Label(name));

        String datesText = birthYear + " -";
        if (deathYear != -1) {
            datesText += " " + deathYear;
        }
        nameDates.add(new Label(datesText));

        imageNameDates.add(nameDates, new GridData(GridData.FILL_HORIZONTAL));

        new FigureMover(this);
    }

    @Override
    public void paintFigure(Graphics graphics) {
        Rectangle r = this.getBounds();
        System.out.println("bounds ===> " + r.toString());
        graphics.setBackgroundPattern(new Pattern(Display.getCurrent(), r.x,
                r.y, r.x + r.width, r.y + r.height, ColorConstants.white,
                ColorConstants.lightGray));
        graphics.fillRectangle(r);
    }
}
